<template>
  <a-card
    class="general-card"
    :header-style="{ paddingBottom: '16px' }"
    :body-style="{ paddingBottom: '12px' }"
  >
    <template #title>
      <div class="card-header">
        <icon-star-fill class="card-icon" />
        <span class="card-title">收藏帖子</span>
      </div>
    </template>
    <a-list
      v-if="!loading"
      :max-height="300"
      :data="list"
      :bordered="false"
      class="post-list"
      hoverable
      @reach-bottom="handleLoadPost"
    >
      <template #item="{ item, index }">
        <a-list-item
          :key="index"
          class="post-item"
          action-layout="vertical"
          @click="handleClickItem(item)"
        >
          <a-list-item-meta :title="item.title">
            <template #avatar>
              <a-popover position="right">
                <template #content>
                  <div class="user-popover">
                    <a-avatar :image-url="item.avatar || '/default-avatar.png'" />
                    <div class="user-info">
                      <div class="user-name">{{ item.creatorName }}</div>
                      <div class="user-intro">{{ item.introduce || '这个用户很懒，还没有介绍自己' }}</div>
                    </div>
                  </div>
                </template>
                <a-avatar :image-url="item.avatar || '/default-avatar.png'" />
              </a-popover>
            </template>
          </a-list-item-meta>
        </a-list-item>
      </template>
      <template #empty>
        <a-empty description="暂无收藏帖子" />
      </template>
    </a-list>
    <a-skeleton v-if="loading" :animation="true">
      <a-skeleton-line :rows="5" />
    </a-skeleton>
  </a-card>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { OjPostVo, Paging } from '@/api/gen-api';
import { Message } from '@arco-design/web-vue';
import { useRoute } from 'vue-router';
import router from '@/router';
import { OjFavourPostService } from '@/api/gen-api/services/OjFavourPostService.ts';

const list = ref<OjPostVo[]>([]);
const totalPage = ref();
const paging = ref<Paging>({
  pageNum: 1,
  pageSize: 10
});
const loading = ref(false);
const userId = useRoute().query.id as string;

const getPostList = () => {
  loading.value = true;
  OjFavourPostService.getFavourPost(paging.value, userId)
    .then(res => {
      if (res.result.records) {
        totalPage.value = res.result.totalPage;
        list.value.push(...res.result.records);
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  getPostList();
});

const handleLoadPost = () => {
  if (paging.value.pageNum >= totalPage.value) {
    // Message.warning('没有更多了');
  } else {
    paging.value.pageNum++;
    getPostList();
  }
};

const handleClickItem = (item: OjPostVo) => {
  router.push({
    name: 'PostInfo',
    query: {
      postId: item.id
    }
  });
};
</script>

<style scoped lang="less">
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-icon {
  color: #ffb800;
  font-size: 18px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-1);
}

.post-list {
  padding: 0 4px;
}

.post-item {
  transition: all 0.3s ease;
  padding: 10px 12px;
  border-radius: 6px;
  margin: 4px 0;

  &:hover {
    cursor: pointer;
    background-color: rgba(255, 184, 0, 0.05);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  }

  :deep(.arco-list-item-meta-title) {
    font-weight: 500;
    font-size: 14px;
    color: var(--color-text-1);
    transition: color 0.3s ease;
    margin-bottom: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &:hover {
    :deep(.arco-list-item-meta-title) {
      color: #ff9a00;
    }
  }

  :deep(.arco-avatar) {
    border: 1px solid #f0f0f0;
    transition: transform 0.3s ease;
  }

  &:hover {
    :deep(.arco-avatar) {
      transform: scale(1.05);
    }
  }
}

.user-popover {
  display: flex;
  gap: 12px;
  padding: 4px;
  max-width: 240px;

  .arco-avatar {
    width: 40px;
    height: 40px;
  }

  .user-info {
    display: flex;
    flex-direction: column;
    gap: 4px;

    .user-name {
      font-weight: 600;
      font-size: 15px;
      color: var(--color-text-1);
    }

    .user-intro {
      font-size: 13px;
      color: var(--color-text-3);
      max-width: 170px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
  }
  }
}

// 自定义骨架屏加载样式
:deep(.arco-skeleton) {
  padding: 8px 12px;

  .arco-skeleton-line-row {
    margin-bottom: 16px;
  }
}

:deep(.arco-empty) {
  padding: 20px 0;
}
</style>
